package cn.test.demo.base_demo.config.springSecurity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author 王杨帅
 * @create 2018-05-27 21:27
 * @desc
 **/
@Configuration
public class FurySpringSecurityConfig extends WebSecurityConfigurerAdapter {

    /** 依赖注入自定义的登录成功处理器 */
    @Autowired
    private FuryAuthenticationSuccessHandler furyAuthenticationSuccessHandler;

    /** 依赖注入自定义的登录失败处理器 */
    @Autowired
    private FuryAuthenticationFailureHandler furyAuthenticationFailureHandler;

//    向spring容器中创建一个Bean
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginProcessingUrl("/furyLogin") // 登录请求路径
                .successHandler(furyAuthenticationSuccessHandler) // 验证成功处理器
                .failureHandler(furyAuthenticationFailureHandler) // 验证失败处理器
                .and().authorizeRequests()
                        .antMatchers("/furyLogin").permitAll() // 登录请求路径不进行过滤
                        .anyRequest()
                        .authenticated()
                .and().csrf().disable(); // 取消跨站请求伪造防护
    }

}

